package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.feign.BehaviorFeign;
import com.usian.article.feign.UserFeign;
import com.usian.article.mapper.ApAuthorMapper;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.service.ArticleConfigService;
import com.usian.model.article.ApAuthor;
import com.usian.model.article.ApCollection;
import com.usian.model.article.GetConfigDto;
import com.usian.model.base.BaseResult;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleConfigServiceImpl implements BaseResult, ArticleConfigService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private ApAuthorMapper authorMapper;
    @Autowired
    private BehaviorFeign behaviorFeign;
    @Autowired
    private ApCollectionMapper collectionMapper;
    @Override
    public ResponseResult getConfig(GetConfigDto dto) {
        if (dto == null || dto.getArticleId() == null || dto.getAuthorId() == null)return paramError;
        Integer userId = ThreadLocalUtils.getUserId();
        if (userId == null)return needLogin;
        //查询关注的结果
        Integer followId = authorMapper.selectById(dto.getAuthorId()).getUserId();
        Boolean isFollow = userFeign.getResult(userId, followId);
        //查询点赞的结果
        Boolean likeStatus = behaviorFeign.getLikeStatus(userId, dto.getArticleId());
        //查询收藏的状态
        Integer entryId = behaviorFeign.getEntry(userId).getId();
        ApCollection collection = collectionMapper.selectOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, entryId).eq(ApCollection::getArticleId, dto.getArticleId()));
        Boolean isCollection = collection != null;
        //讲集中结果统一给前端返回
        Map<String,Boolean> result = new HashMap<>();
        result.put("isFollow",isFollow);
        result.put("likeStatus",likeStatus);
        result.put("isCollection",isCollection);
        return success(result);
    }
}
